package BandB.Tool.GPhoneManager.TaskManager.Util;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;
import android.util.Log;

public class DataProvider extends ContentProvider {

	private static final String DATABASE_NAME = "blackApps";
	private static final int DATABASE_VERSION = 2;
	static final String TABLE_NAME = "appInfo";

	public static final String AUTHORITY = "BandB.Tool.GPhoneManager.Taskiller.provider";
	public static final Uri APPLICATIONS_CONTENT_URI = Uri.parse("content://"
			+ DataProvider.AUTHORITY + "/" + DataProvider.TABLE_NAME);
	public static final int URI_CODE = 1;

	// columns for applications table
	public static final String APP_PACKAGE_NAME = "PackageName";
	public static final String APP_NAME = "ApplicationName";
	// public static final String APP_BLACK_OR_NOT = "isBlack";

	private static final UriMatcher sUriMatcher;

	public static final String APPLICATION_TYPE = "vnd.android.cursor.dir/vnd.com.mgeek.android.Taskmanager.news";

	public static String[] APP_PROJECTION = { APP_PACKAGE_NAME, APP_NAME };

	private DatabaseHelper mOpenHelper;

	// private final String TAG = "RSSProvider";

	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		// TODO Auto-generated method stub
		SQLiteDatabase db = mOpenHelper.getWritableDatabase();
		int count = 0;
		switch (sUriMatcher.match(uri)) {
		case URI_CODE:
			count = db.delete(TABLE_NAME, selection, selectionArgs);
			break;
		default:
			throw new IllegalArgumentException("Unknown URI " + uri);
		}

		getContext().getContentResolver().notifyChange(uri, null);
		return count;
	}

	@Override
	public String getType(Uri uri) {
		// TODO Auto-generated method stub
		switch (sUriMatcher.match(uri)) {
		case URI_CODE:
			return APPLICATION_TYPE;
		default:
			throw new IllegalArgumentException("Unknown URI " + uri);
		}
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		// TODO Auto-generated method stub
		SQLiteDatabase db = mOpenHelper.getWritableDatabase();
		switch (sUriMatcher.match(uri)) {
		case URI_CODE:
			db.insert(DataProvider.TABLE_NAME, null, values);
			return uri;
		default:
			throw new IllegalArgumentException("Unknown URI " + uri);
		}
	}

	@Override
	public boolean onCreate() {
		// TODO Auto-generated method stub
		mOpenHelper = new DatabaseHelper(getContext());
		return true;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) {
		// TODO Auto-generated method stub
		SQLiteDatabase db = mOpenHelper.getWritableDatabase();
		Cursor c = null;
		switch (sUriMatcher.match(uri)) {
		case URI_CODE:
			c = db.query(DataProvider.TABLE_NAME, projection, selection,
					selectionArgs, null, null, sortOrder);
			c.setNotificationUri(getContext().getContentResolver(), uri);
			return c;
		default:
			throw new IllegalArgumentException("Unknown URI " + uri);
		}
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection,
			String[] selectionArgs) {
		// TODO Auto-generated method stub
		SQLiteDatabase db = mOpenHelper.getWritableDatabase();
		int count = 0;
		switch (sUriMatcher.match(uri)) {
		case URI_CODE:
			count = db.update(TABLE_NAME, values, selection, selectionArgs);
			getContext().getContentResolver().notifyChange(uri, null);
			return count;
		default:
			throw new IllegalArgumentException("Unknown URI " + uri);
		}
	}

	private static class DatabaseHelper extends SQLiteOpenHelper {

		public static final String TAG = "DataProvider";

		DatabaseHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			Log.e(TAG, "create table start");
			String sql1 = new StringBuilder().append(
					"CREATE TABLE " + TABLE_NAME + " (").append(
					DataProvider.APP_PACKAGE_NAME).append(" TEXT PRIMARY KEY,")
					.append(APP_NAME).append(" TEXT);").toString();
			db.execSQL(sql1);
			Log.e(TAG, "create table end");
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
			onCreate(db);
		}
	}

	static {
		sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
		sUriMatcher.addURI(AUTHORITY, TABLE_NAME, URI_CODE);
	}
}
